<?php
$T->loadTpl("./html/".$template."/".$qname.".html");
$T->SetTpl('cssjs','html/cssjs.inc'); 
$T->SetTpl('jsmodel','html/jsmodel.inc');     
  
$emailbox=isset($_GET['emailbox'])?$_GET['emailbox']:'sj';
$state=isset($_GET['state'])?$_GET['state']:'-1';
$start_time=isset($_GET['start_time'])?$_GET['start_time']:'';
$end_time=isset($_GET['end_time'])?$_GET['end_time']:'';
$sender=isset($_GET['sender'])?htmlspecialchars($_GET['sender'],ENT_QUOTES):'';
$subject=isset($_GET['subject'])?htmlspecialchars($_GET['subject'],ENT_QUOTES):'';
$p=isset($_GET['p'])?$_GET['p']:'0';
if($p<1)$p=1;
$wh=" 1=1 ";
//p(strtotime($start_time));p(strtotime($end_time)+24*3600);
if($start_time!=''&&$end_time!=''){
	$start_time_str=strtotime($start_time);
	$end_time_str=strtotime($end_time)+24*3600;
	$wh.=" and (A.`timestamp` between {$start_time_str} and {$end_time_str}) ";
}
if($start_time!=''&&$end_time==''){
	$start_time_str=strtotime($start_time);
	$wh.=" and A.`timestamp`>{$start_time_str} ";
}
if($start_time==''&&$end_time!=''){
	$end_time_str=strtotime($end_time)+24*3600;
	$wh.=" and A.`timestamp`<{$end_time_str} ";
}
$wh.=($subject=='')?"":" and A.subject like '%{$subject}%' ";
if($emailbox=='sj'){
	$html="";
	$wh.=" and A.receiver='{$_SESSION['uid']}' and A.delete_flag=0 and A.send_flag!=0 ";
	$wh.=($state=='-1')?"":" and A.read_flag={$state} ";
	$wh.=($sender=='')?"":" and B.truename like '%{$sender}%' ";
	$sql="select A.*,from_unixtime(A.`timestamp`,'%Y-%m-%d %H:%i:%s') as etime,B.truename from sys_email A left join sys_member B on A.sender=B.id where {$wh} order by A.read_flag asc,A.`timestamp` desc limit ".(($p-1)*15).",15";
    
    $email_info=$T->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
	foreach($email_info as $k=>$email){
		$email['attachment_num']=($email['attachment']=='')?"":count(explode(",", $email['attachment']))."个附件";
		$email['important_des']=($email['important']=='0')?"":(($email['important']=='1')?"重要":"非常重要");
		$email['read_flag_des']=($email['read_flag']=='0')?'<span style="color:#c00;">未读</span>':'<span style="">已读</span>';
		$html.=<<<EOF
		    <tr>
                <td>
                <input type="checkbox" name="checkbox" lay-skin="primary" value="{$email['id']}"/>
                </td>
                <td>{$email['id']}</td>
                <th>{$email['truename']}</th>
				<th>{$email['subject']}</th>
				<th>{$email['attachment_num']}</th>
				<th>{$email['etime']}</th>
				<th style="color:#c00;">{$email['important_des']}</th>
                <td>{$email['read_flag_des']}</td>
                <td>
                	<button class="layui-btn layui-btn-mini layui-btn-normal modal-iframe" data-opt="view" data-id="eid={$email['id']}&emailbox=sj&read_flag={$email['read_flag']}">
                        <i class="iconfont">&#xe605;</i>查看
                    </button>
                    <button class="layui-btn layui-btn-mini layui-btn-danger ajax" data-opt="del"  data-id="{$email['id']}">
                        <i class="iconfont">&#xe626;</i>删除
                    </button></td>
                </td>
            </tr>
EOF;
	}
    $rc=$T->db->query("select count(1) from sys_email A left join sys_member B on A.sender=B.id where {$wh}")->fetchColumn(0);
    $T->Set("html",$html);
}
if($emailbox=='fj'){
	$html="";
	$wh.=" and A.sender='{$_SESSION['uid']}' and A.delete_flag=0 and A.send_flag=0 ";
	$sql="select A.*,from_unixtime(A.`timestamp`,'%Y-%m-%d %H:%i:%s') as etime from sys_email A where {$wh} order by A.read_flag asc,A.`timestamp` desc limit ".(($p-1)*15).",15";
    //p($sql);
    $email_info=$T->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
	foreach($email_info as $k=>$email){
		$receiver=$email['receiver'];
		if($receiver=='')continue;
		$receiver=explode(",", $receiver);
		foreach($receiver as $k=>$v){
			$receiver[$k]="'".$v."'";
		}
		$receiver=implode(",", $receiver);
		$receiver_info=$T->db->query("select truename from sys_member where id in({$receiver})")->fetchAll(PDO::FETCH_COLUMN);
		$receiver_read=$T->db->query("select B.truename from sys_email A left join sys_member B on A.receiver=B.id where A.send_flag={$email['id']} and A.read_flag=1")->fetchAll(PDO::FETCH_COLUMN);//已读的用户信息
	    $email['receiver']=implode(",", $receiver_info);
		$email['receiver_limit']=mb_get_str(0,50,$email['receiver']);
		$email['receiver_read']=implode(",", $receiver_read);
		$email['receiver_read_limit']=mb_get_str(0,50,$email['receiver_read']);
		$html.=<<<EOF
		    <tr>
			    <td>
			    <input type="checkbox" name="checkbox" lay-skin="primary" value="{$email['id']}"/>
			    </td>
			    <td>{$email['id']}</td>
			    <th title="{$email['receiver']}">{$email['receiver_limit']}</th>
				<th>{$email['subject']}</th>
				<th>{$email['etime']}</th>
				<th title="{$email['receiver_read']}">{$email['receiver_read_limit']}</th>
			    <td>
			    	<button class="layui-btn layui-btn-mini layui-btn-normal modal-iframe" data-opt="view" data-id="eid={$email['id']}&emailbox=fj">
			            <i class="iconfont">&#xe605;</i>查看
			        </button>
			        <button class="layui-btn layui-btn-mini layui-btn-danger ajax" data-opt="del"  data-id="{$email['id']}">
			            <i class="iconfont">&#xe626;</i>删除
			        </button></td>
			    </td>
		    </tr>
EOF;
	}
    $rc=$T->db->query("select count(1) from sys_email A where {$wh}")->fetchColumn(0); 
    $T->Set("html",$html);
    
}
if($emailbox=='lj'){
	$html="";
	$wh.=($sender=='')?"":" and B.truename like '%{$sender}%' ";
	$wh.=" and ((A.sender='{$_SESSION['uid']}' and A.send_flag=0) or (A.receiver='{$_SESSION['uid']}' and A.send_flag!=0)) and A.delete_flag=1";
	$sql="select A.*,from_unixtime(A.`timestamp`,'%Y-%m-%d %H:%i:%s') as etime,B.truename from sys_email A left join sys_member B on A.sender=B.id where {$wh} order by A.`timestamp` desc limit ".(($p-1)*15).",15";
    //p($sql);
    $email_info=$T->db->query($sql)->fetchAll(PDO::FETCH_ASSOC);
	foreach($email_info as $k=>$email){
		
		$html.=<<<EOF
		    <tr>
                <td>
                <input type="checkbox" name="checkbox" lay-skin="primary" value="{$email['id']}"/>
                </td>
                <td>{$email['id']}</td>
                <th>{$email['truename']}</th>
				<th>{$email['subject']}</th>
				<th>{$email['etime']}</th>
                <td>
                	<button class="layui-btn layui-btn-mini layui-btn-normal modal-iframe" data-opt="view" data-id="eid={$email['id']}&emailbox=lj">
                        <i class="iconfont">&#xe605;</i>查看
                    </button>
                    <button class="layui-btn layui-btn-mini modal-iframe" data-opt="renew" data-id="{$email['id']}">
                        <i class="iconfont">&#xe653;</i>恢复
                    </button>
                    <button class="layui-btn layui-btn-mini layui-btn-danger ajax" data-opt="del"  data-id="{$email['id']}">
                        <i class="iconfont">&#xe626;</i>彻底删除
                    </button></td>
                </td>
            </tr>
EOF;
	}
    $rc=$T->db->query("select count(1) from sys_email A left join sys_member B on A.sender=B.id where {$wh}")->fetchColumn(0); 
    $T->Set("html",$html);
    
}
/*if($emailbox=='lj'){
	$wh.=" A.sender='{$_SESSION['uid']}' and A.delete_flag=0";
	$sql="select A.*,from_unixtime(A.timestamp,'%Y-%m-%d %H:%i:%s') as etime from sys_email A where {$wh} order by A.read_flag asc,A.`timestamp` desc";
}*/
//session_unset();
//session_destroy();
//exit;
//p($_SESSION);
//$sql="select A.*,from_unixtime(A.timestamp,'%Y-%m-%d %H:%i:%s') as etime from sys_email A left join sys_member B on A.sender=B.id where A.sender='".$_SESSION['uid']."' and send_flag=0 order by `timestamp`";
$page=getPageHtml_bt($rc,15,$p,"&t=email_sys&emailbox={$emailbox}&state={$state}&start_time={$start_time}&end_time={$end_time}&sender={$sender}&subject={$subject}");
$page=mb_convert_encoding($page, 'utf-8', 'utf-8');
$T->Set("page",$page);
$T->Set("emailbox",$emailbox);
$T->Set("state",$state);
$T->Set("start_time",$start_time);
$T->Set("end_time",$end_time);
$T->Set("sender",$sender);
$T->Set("subject",$subject);
		
$T->Set("template",$template);         
$T->clearNaN();       
$T->clearNoN();
$T->display(); 